<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2022/3/29
 * Time: 15:51
 */

namespace app\api\controller;

use app\project\service\PublicService;
use think\Config;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;
class Statistics extends Controller
{
    //招商引资类（在谈项目分类）
    public function censusapi(){

        $common = new Common();
        if(!empty($_POST['uid'])){
            $project_user = $common->testusername($_POST['uid']);
        }else{
            $project = new Project();
            $project_user = $project->iflogin();
        }

        try{
            $library = empty($_POST['library']) ? 0 : $_POST['library'];
            $sign = empty($_POST['sign']) ? 0 : $_POST['sign'];

            //查询我负责的项目
            $name = $project_user['name'];
            $pro_v1 = $project->conscientious($name,1);
            //查询我关联的项目
            $project_ids = $project->associated($project_user['id']);
            //获取我负责的项目

            $project_ids = implode($project_ids,",");
            if(in_array($project_user['id'],[4,130,7,8,182,2,188,189,190,201,267])){
                $where = "status=1";
            }else{
                $where = "(id in (".$project_ids.") or duty_person like '%".$name."%') and status=1";
            }
            $where .= " and library=".$library." and sign=".$sign;

            unset($dictionaries);
            $Industrial = [['name'=>"产业链","value"=>2]];
            foreach ($Industrial as $key=>$item){
                $classWhere['type'] = $item["value"];
                $dictionaries = Db::name("project_type")->where($classWhere)->field("id,name,image")->order("sort desc")->select();
                //查询单位下所关联的项目
                foreach ($dictionaries as $k=>$v){
                    unset($Splicing);
                    $Splicing = $where." and IndustrialChain =".$v['id'];
                    $dictionaries[$k]['count'] = Db::name("project_pro")->where($Splicing)->count("*");
                    $dictionaries[$k]['rest'] = mb_substr($v['name'], 0, 1, 'utf-8');
                    $dictionaries[$k]['image'] = "https://".$_SERVER['SERVER_NAME'].$v['image'];

//                    $dictionaries[$k]['image'] = "http://yl.unionstart.work/upload/platform1/202208/430c237f38fc0c767f7db7fd297c3445.png";
                    //dictionaries[$k]['color'] = "#".$this->randColor();
                }

                if($sign==5){
                    unset($dictionaries[6]);
                }

                $Industrial[$key]['list'] = $dictionaries;
            }



            $black['code'] = 0;
            $black['Industrial'] = $Industrial;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['line'] = $e->getLine();
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);

    }


    //招商签约库统计
    public function zhaoshang(){

        try{
            if(!empty($_POST['year'])){
                $year = $_POST['year'];
                $where['addtime'] = ['like',"%{$year}%"];
            }
            //总项目数
            $where['library'] = 1;
            $where['status'] = 1;
            $Count = PublicService::counService("project_pro",$where);
            //计划总投资
            $Investment = PublicService::findCounService("project_pro",$where,"investment");

            //已签约项目数
            $where['sign'] = 2;
            $Sign = PublicService::counService("project_pro",$where);
            $InvestmentSign = PublicService::findCounService("project_pro",$where,"investment");

            //未签约项目数
            $where['sign'] = 1;
            $SignUn = PublicService::counService("project_pro",$where);
            $InvestmentSignUn = PublicService::findCounService("project_pro",$where,"investment");

            //已逾期项目数
            $where['sign'] = 3;
            $Overdue= PublicService::counService("project_pro",$where);
            $InvestmentOverdue = PublicService::findCounService("project_pro",$where,"investment");

            //无法签约项目数
            $where['sign'] = 4;
            $SignUnable = PublicService::counService("project_pro",$where);
            $InvestmentSignUnable = PublicService::findCounService("project_pro",$where,"investment");

            $black['code'] = 0;
            $black['Count'] = $Count;
            $black['Investment'] = $Investment;
            $black['Sign'] = $Sign;
            $black['InvestmentSign'] = $InvestmentSign;
            $black['SignUn'] = $SignUn;
            $black['InvestmentSignUn'] = $InvestmentSignUn;
            $black['Overdue'] = $Overdue;
            $black['InvestmentOverdue'] = $InvestmentOverdue;
            $black['SignUnable'] = $SignUnable;
            $black['InvestmentSignUnable'] = $InvestmentSignUnable;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);

    }

    //预备库统计
    public function yubei(){
        try{
            if(!empty($_POST['year'])){
                $year = $_POST['year'];
                $where['addtime'] = ['like',"%{$year}%"];
            }
            //总项目数
            $where['library'] = 2;
            $where['status'] = 1;
            $Count = PublicService::counService("project_pro",$where);
            //计划总投资
            $Investment = PublicService::findCounService("project_pro",$where,"investment");

            //政府投资项目数
            $where['invest_type'] = 1;
            $government = PublicService::counService("project_pro",$where);
            $governmentInvestment = PublicService::findCounService("project_pro",$where,"investment");

            //社会投资项目数
            $where['invest_type'] = 2;
            $society = PublicService::counService("project_pro",$where);
            $societyInvestment = PublicService::findCounService("project_pro",$where,"investment");


            unset($where['invest_type']);
            //已开工项目数
            $where['start_status'] = 2;
            $start= PublicService::counService("project_pro",$where);
            $startInvestment = PublicService::findCounService("project_pro",$where,"investment");

            //待开工项目数
            $where['start_status'] = 1;
            $stay = PublicService::counService("project_pro",$where);
            $stayInvestment = PublicService::findCounService("project_pro",$where,"investment");

            //已逾期项目数
            $where['start_status'] = 3;
            $overdue = PublicService::counService("project_pro",$where);
            $overdueInvestment = PublicService::findCounService("project_pro",$where,"investment");

            //已终止项目数
            $where['start_status'] = 4;
            $termination = PublicService::counService("project_pro",$where);
            $terminationInvestment = PublicService::findCounService("project_pro",$where,"investment");

            $black['code'] = 0;
            $black['Count'] = $Count;
            $black['Investment'] = $Investment;
            $black['government'] = $government;
            $black['governmentInvestment'] = $governmentInvestment;
            $black['society'] = $society;
            $black['societyInvestment'] = $societyInvestment;
            $black['start'] = $start;
            $black['startInvestment'] = $startInvestment;
            $black['stay'] = $stay;
            $black['stayInvestment'] = $stayInvestment;
            $black['overdue'] = $overdue;
            $black['overdueInvestment'] = $overdueInvestment;
            $black['termination'] = $termination;
            $black['terminationInvestment'] = $terminationInvestment;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //开工库统计
    public function kaigong(){
        try{
            if(!empty($_POST['year'])){
                $year = $_POST['year'];
                $where['addtime'] = ['like',"%{$year}%"];
            }

            $type = empty($_POST['type'])?0:$_POST['type'];

            if($type==1){
                $where['project_status'] = ['in',[0,2]];
            }

            if($type==2){
                $where['project_status'] = 1;
                $where['library'] = 3;
                $where['status'] = 1;
                //总项目数
                $black['Count'] = PublicService::counService("project_pro", $where);
                //计划总投资
                $black['Investment'] = PublicService::findCounService("project_pro", $where, "investment")/10000;


                //政府投资
                $where['invest_type'] = 1;
                $black['government'] = PublicService::counService("project_pro", $where);
                //民间投资
                $where['invest_type'] = 2;
                $black['folk'] = PublicService::counService("project_pro", $where);

                unset($where['invest_type']);
                $year = empty($_POST['year'])?date("Y"):$_POST['year'];
                $where['addtime'] = ['like',"%{$year}%"];
                //当年新竣工
                $black['thisCount'] = PublicService::counService("project_pro", $where);
                $black['thisInvestment'] = PublicService::findCounService("project_pro", $where, "investment")/10000;
            }

            if($type!=2) {
                //总项目数
                $where['library'] = 3;
                $where['status'] = 1;
                $black['Count'] = PublicService::counService("project_pro", $where);
                //计划总投资
                $black['Investment'] = PublicService::findCounService("project_pro", $where, "investment")/10000;

                //政府投资
                $where['invest_type'] = 1;
                $black['government'] = PublicService::counService("project_pro", $where);
                //计划总投资
                $black['governmentInvestment'] = PublicService::findCounService("project_pro", $where, "investment")/10000;

                //民间投资
                $where['invest_type'] = 2;
                $black['folk'] = PublicService::counService("project_pro", $where);
                //计划总投资
                $black['folkInvestment'] = PublicService::findCounService("project_pro", $where, "investment")/10000;

                if ($type == 0) {
                    unset($where['invest_type']);
                    //已入库项目数
                    $where['warehousing'] = 2;
                    $black['warehousing'] = PublicService::counService("project_pro", $where);
                    //未入库项目数
                    $where['warehousing'] = 1;
                    $black['warehousingUn'] = PublicService::counService("project_pro", $where);
                }
            }

            if($type==1){
                unset($where['project_status']);
                $where['project_status'] = 0;//基础施工
                $black['basics'] = PublicService::counService("project_pro",$where);
                $where['project_status'] = 2;//主体施工
                $black['subject'] = PublicService::counService("project_pro",$where);
            }


            $black['code'] = 0;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //入统库统计
    public function rutong(){
        try{
            if(!empty($_POST['year'])){
                $year = $_POST['year'];
                $where['addtime'] = ['like',"%{$year}%"];
            }
            $where['library'] = 5;
            $where['status'] = 1;
            //总项目数
            $black['Count'] = PublicService::counService("project_pro", $where);
            //计划总投资
            $black['investment'] = PublicService::findCounService("project_pro", $where, "investment")/10000;

            //已报揭项目数
            $where['reply'] = 2;
            $black['reported'] = PublicService::counService("project_pro", $where);

            //剩余投资
            $black['reportedInvestment'] = 0;

            $black['code'] = 0;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //双千库
    public function shuangqian(){
        try{
            $year = empty($_POST['year'])?date("Y"):$_POST['year'];
            $where['a.addtime'] = ['like',"%{$year}%"];
            $where['b.library'] = 6;
            $where['b.status'] = 1;

            $typeWhere['type'] = 6;
            $typeWhere['status'] = 1;
            $type = Db::name("project_type")->where($typeWhere)->field("id,name")->order("sort desc,id desc")->select();

            //全年走访次数
            $black['yearCount'] = Db::name("project_pro_figure as a")->join("project_pro b","a.pro_id = b.id")->where($where)->count("*");

            //按问题分类统计
            $yearGroup = Db::name("project_pro_figure as a")->join("project_pro b","a.pro_id = b.id")->where($where)->field("a.problem_id,count(*) as count")->group("a.problem_id")->select();
            $yearGroup = array_column($yearGroup,"count","problem_id");
            $yearType = [];
            foreach ($type as $k=>$v){
                $count = empty($yearGroup[$v['id']])?0:$yearGroup[$v['id']];
                $yearType[] = ['name'=>$v['name'],'count'=>$count];
            }
            $black['yearType'] = $yearType;

            //本月走访次数,strtotime('-2 month')
            $month = date("Y-m");
            $where['a.addtime'] = ['like',"%{$month}%"];
            $black['monthCount'] = Db::name("project_pro_figure as a")->join("project_pro b","a.pro_id = b.id")->where($where)->count("*");

            //按问题分类统计
            $monthGroup = Db::name("project_pro_figure as a")->join("project_pro b","a.pro_id = b.id")->where($where)->field("a.problem_id,count(*) as count")->group("a.problem_id")->select();
            $monthGroup = array_column($monthGroup,"count","problem_id");
            $monthType = [];
            foreach ($type as $k=>$v){
                $count = empty($monthGroup[$v['id']])?0:$monthGroup[$v['id']];
                $monthType[] = ['name'=>$v['name'],'count'=>$count];
            }
            $black['monthType'] = $monthType;

            $black['code'] = 0;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //重点库
    public function zhongdian(){
        try{
            if(!empty($_POST['year'])){
                $year = $_POST['year'];
                $where['addtime'] = ['like',"%{$year}%"];
            }
            $where['library'] = 4;
            $where['status'] = 1;
            //总项目数
            $black['Count'] = PublicService::counService("project_pro", $where);
            //县级领导联系项目
            $where['leader'] = ['neq',''];
            $black['leaderCount'] = PublicService::counService("project_pro", $where);

            unset($where['leader']);
            //已开工项目数
            $black['startCount'] = 0;

            //已完工项目数
            $where['project_status'] = 2;
            $black['closingCount'] = PublicService::counService("project_pro", $where);
            //推进中项目数
            $where['project_status'] = 0;
            $black['advancingCount'] = PublicService::counService("project_pro", $where);
            //暂缓推进中项目数
            $where['project_status'] = 3;
            $black['suspendCount'] = PublicService::counService("project_pro", $where);


            //项目类别
//            $typeWhere['type'] = 1;
//            $typeList = PublicService::ListService("project_type",$typeWhere,"id,name");

            unset($where['project_status']);
            //社会投资
            $where['project_type'] = 2;
            $black['societyCount'] = PublicService::counService("project_pro", $where);
            //社会投资(总投资)
            $societyInvestment = PublicService::findCounService("project_pro", $where, "investment");
            $black['societyInvestment'] = $societyInvestment;

            //5亿元以上项目
            $where['investment'][] = ['egt',50000];
            $where['investment'][] = ['lt',100000];
            $black['oneCount'] = PublicService::counService("project_pro", $where);

            unset($where['investment']);
            //10亿元以上项目
            $where['investment'][] = ['egt',100000];
            $where['investment'][] = ['lt',200000];
            $black['twoCount'] = PublicService::counService("project_pro", $where);

            unset($where['investment']);
            //20亿元以上项目
            $where['investment'][] = ['egt',200000];
            $where['investment'][] = ['lt',500000];
            $black['threeCount']  = PublicService::counService("project_pro", $where);

            unset($where['investment']);
            //50亿元以上项目
            $where['investment'] = ['egt',500000];
            $black['fourCount'] = PublicService::counService("project_pro", $where);
            //社会投资end

            unset($where['investment']);
            //政府投资
            $where['project_type'] = 3;
            $black['governmentCount'] = PublicService::counService("project_pro", $where);
            //政府投资(总投资)
            $governmentInvestment = PublicService::findCounService("project_pro", $where, "investment");
            $black['governmentInvestment'] = $governmentInvestment;

            //5亿元以上项目
            $where['investment'][] = ['egt',50000];
            $where['investment'][] = ['lt',100000];
            $black['oneGovernment'] = PublicService::counService("project_pro", $where);

            unset($where['investment']);
            //10亿元以上项目
            $where['investment'][] = ['egt',100000];
            $where['investment'][] = ['lt',200000];
            $black['twoGovernment'] = PublicService::counService("project_pro", $where);

            unset($where['investment']);
            //20亿元以上项目
            $where['investment'][] = ['egt',200000];
            $where['investment'][] = ['lt',500000];
            $black['threeGovernment']  = PublicService::counService("project_pro", $where);

            unset($where['investment']);
            //50亿元以上项目
            $where['investment'] = ['egt',500000];
            $black['fourGovernment'] = PublicService::counService("project_pro", $where);
            //政府投资end

            $black['code'] = 0;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

}